package cn.com.connext.scrm.service;

import cn.com.connext.scrm.domain.master.*;
import cn.com.connext.scrm.repository.master.DwaConvertCodeRepository;
import cn.com.connext.scrm.repository.master.DwaGetCouponRepository;
import cn.com.connext.scrm.repository.master.PrizeRepository;
import cn.com.connext.scrm.repository.master.UploadCodeRepository;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.service.mapper.TmpMsgRecordService;
import cn.com.connext.scrm.web.rest.vm.DwaIsReceiveVM;
import cn.com.connext.scrm.web.rest.vm.TempSubScribeVm;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class UploadCodeService {
    @Autowired
    private UploadCodeRepository uploadCodeRepository;
    @Autowired
    private PrizeGoodService prizeGoodService;

    public void addAllCode(List<PrizeCode> codeList,Integer prizeGoodId,Integer size) {
//      if(codeList !=null && codeList.size() >0){
//          for (PrizeCode code:codeList) {
//              uploadCodeRepository.saveAndFlush(code);
//          }
//      }
       uploadCodeRepository.save(codeList);
        PrizeGood pd = prizeGoodService.findById(prizeGoodId);
//        pd.setId(prizeGoodId);
        int stock = pd.getStock() == null ? 0 : pd.getStock();
        pd.setStock(stock+size);
        prizeGoodService.updatePrizeGood(pd);
    }

    public List<String> findAllCode() {
        return uploadCodeRepository.findAllCode();
    }

    public PrizeCode findRandomCodeNoUse(Integer id) {
        return uploadCodeRepository.findNoUseCode(id);
    }

    public void edit(PrizeCode code) {
        uploadCodeRepository.save(code);
    }

    public  List<PrizeCode> findByGoodIdAndUnionId(Integer goodId, String unionId) {
        return uploadCodeRepository.findByGoodIdAndUnionId(goodId,unionId);
    }

    public PrizeCode findPrizeCodeByGoodIdAndUnionId(Integer goodId, String unionId) {
        return uploadCodeRepository.findPrizeCodeByGoodIdAndUnionId(goodId,unionId);
    }
}
